package com.quan.system.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.quan.commons.core.MyBaseController;
import com.quan.commons.utils.PageUtils;
import com.quan.commons.vo.R;
import com.quan.system.commons.vo.SysDictVo;
import com.quan.system.entity.SysDict;
import com.quan.system.entity.SysDictDetail;
import com.quan.system.service.SysDictDetailService;
import com.quan.system.service.SysDictService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 系统-字典
 *
 * @author yhaoquan
 * @email yhaoquan@163.com
 * @date 2021-01-04 22:24:24
 */
@Slf4j
@Api(tags = "系统-字典")
@RestController
@RequestMapping("/system/sysdict")
public class SysDictController extends MyBaseController {

    @Autowired
    private SysDictService service;

    @Autowired
    private SysDictDetailService detailService;

    /**
     * 保存
     *
     * @param vo
     * @return
     */
    @PostMapping("save")
    public R save(@Valid @RequestBody SysDictVo vo) {
        this.service.save(vo);
        return R.ok();
    }

    /**
     * 修改
     *
     * @param vo
     * @return
     */
    @PostMapping("update")
    public R update(@Valid @RequestBody SysDictVo vo) {
        this.service.updateById(vo);
        return R.ok();
    }

    /**
     * 删除
     *
     * @param ids
     * @return
     */
    @PostMapping("delete")
    public R delete(@RequestBody Long[] ids) {
        if (null != ids && ids.length > 0) {
            this.service.removeByIds(Arrays.asList(ids));
            return R.ok();
        } else {
            return R.failure();
        }
    }

    /**
     * 多条件查询信息详情
     *
     * @param vo
     * @return
     */
    @GetMapping("/info")
    public R info(SysDictVo vo) {
        QueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>(vo);
        SysDict sysDict = this.service.getOne(queryWrapper);
        return R.ok().put("data", sysDict);
    }

    /**
     * 根据ID查询信息详情
     *
     * @param {id}
     * @return
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        SysDict sysDict = this.service.getById(id);
        return R.ok().put("data", sysDict);
    }

    /**
     * 列表查询
     *
     * @param vo
     * @return
     */
    @GetMapping(value = "list")
    public Object list(SysDictVo vo) {
        List<SysDictVo> list = this.service.queryList(new PageUtils(request), vo);
        return R.ok().put("data", list);
    }

    /**
     * 分页查询
     *
     * @param vo
     * @return
     */
    @GetMapping(value = "page")
    public R queryPage(SysDictVo vo) {
        PageUtils page = this.service.queryPage(new PageUtils(request), vo);
        return R.ok().put("data", page);
    }


    /**
     * 根据code获取字典值列表
     * @param vo
     * @return
     */
    @PostMapping(value = "/getDictByCodeMap")
    public Object getDictByCodeMap(@RequestBody SysDictVo vo) {

        Map<String, Object> map = new HashMap<String, Object>() ;

        if(null != vo.getCodes() && vo.getCodes().length > 0) {
            QueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>();
            queryWrapper.in("code", Arrays.asList(vo.getCodes())).select("id", "name", "code", "status");
            List<SysDict> list = this.service.list(queryWrapper);

            for (SysDict dict : list) {
                QueryWrapper<SysDictDetail> wrapperDictDetail = new QueryWrapper<SysDictDetail>();
                wrapperDictDetail.eq("dict_id", dict.getId());
                wrapperDictDetail.orderByDesc("sort");
                wrapperDictDetail.select("id", "label", "value", "selected", "status");

                List<SysDictDetail> dictItems = this.detailService.list(wrapperDictDetail);

                map.put(dict.getCode(), dictItems);
            }
        }
        return R.ok().put("data", map);
    }

    /**
     * 根据code获取字典值列表
     * @param vo
     * @return
     */
    @PostMapping(value = "/getDictByCodeList")
    public Object getDictByCodeList(@RequestBody SysDictVo vo) {

        Map<String, Object> map = new HashMap<String, Object>() ;

        if(null != vo.getCode()) {
            QueryWrapper<SysDict> queryWrapper = new QueryWrapper<SysDict>();
            queryWrapper.eq("code", vo.getCode());
            final SysDict dict = this.service.getOne(queryWrapper);

            QueryWrapper<SysDictDetail> wrapperDictDetail = new QueryWrapper<SysDictDetail>();
            wrapperDictDetail.eq("dict_id", dict.getId());
            wrapperDictDetail.orderByDesc("sort");
            wrapperDictDetail.select("id", "label", "value", "selected", "status");

            List<SysDictDetail> dictItems = this.detailService.list(wrapperDictDetail);

            return R.ok().put("data", dictItems);
        }
        return R.failure();
    }

}
